--定时读取RS485的modbus数据+采集返回的数据，转换成json数据后发给服务器
--发送命令   0x01 0x03 0x00 0x03 0x00 0x01 0x74 0x0A   返回 01 03 02 00 03 F8 45  计算：0x00*256+0x03
--发送服务器格式  [{"sensor_device_id":30 ,"port_id":04 ,"sdata":20}] 

function
	sys.wait(30000)
	local taskname="userTask"
	log.info(taskname,"start")
	local netid =1
	local netmsg ="UART_DATA_TO_NET"..netid
	local uid=1
	local uartmsg ="UART"..uid.."_NEED_SEND"
	local rd,rl=nil,0
	local cmd={
		{0x01,0x03,0x00,0x03,0x00,0x01,0x74,0x0A}
	}
	local ncmd=nil
	local crcr=nil
	prouart.ProuartStopProReciveCache(1)
	pronet.PronetStopProNetRecive(1)
	while true do	
		while true do 
			rd = prouart.ProuartGetReciveChaceAndDel(uid)
			if rd ==nil then 
				break
			end 
		end
		
		for i=1,#cmd,1 do 
			ncmd=usertool.ToolTableToHexStr(cmd[i])
			prouart.ProuartSetSendChace(uid,ncmd)
			sys.wait(500)
			rd = prouart.ProuartGetReciveChaceAndDel(uid)
			if rd then 
				log.info(taskname,"cmd num=",i,"rd.len",string.len(rd),"rd.len=",string.toHex(rd))
				rd = usertool.ToolHexStrToTable(rd)
				if rd then 
					rl =  #rd
					if rl== 7 then
						log.info(taskname,"after ToolHexStrToTable,rl=",rl,"data is ")
						usertool.ToolDebugPrintfHexTable(rd, rl)				
						crcr = usertool.ToolCheckModbusCRC16(rd, rl,0)
						if crcr == 1 then 
						local data1 =rd[4]*256+rd[5]
						end
					end 
				end 
			end 
		end 
		local d={}
		d.sensor_device_id="30"
		d.port_id="04"
		d.sdata=data1							
		local restr=nil
		restr = json.encode(d)
									
		log.info("taskname.json",d)
		if restr then 
			if 1==pronet.PronetGetNetState(netid) then 
				pronet.PronetInsertSendChache(netid,restr)
				sys.publish(netmsg)
			end 
		end 
		if 1==pronet.PronetGetNetState(netid) then 
			pronet.PronetInsertSendChache(netid,restr)
			sys.publish(netmsg)
		end 		
		sys.wait(1000)
	end
end
